$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'lanling/signupsubject/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 30, key: true, hidden: true},
            // { label: '学校id', name: 'schoolId', index: 'school_id', width: 80, align: "center" },
            {label: '排序号', name: 'sortOrder', index: 'sort_order', width: 80, align: "center"},
            {label: '科目名称', name: 'signSubjectName', index: 'sign_subject_name', width: 200, align: "center"},
            {label: '科目编号', name: 'subjectNumbering', index: 'subject_numbering', width: 80, align: "center"},
            {label: '报名名额', name: 'subjectQuota', index: 'subject_quota', width: 60, align: "center"},
            // {label: '科目详情', name: 'subjectDetail', index: 'subject_detail', width: 80, align: "center"},
            {label: '科目价格', name: 'subjectPrice', index: 'subject_price', width: 80, align: "center"},
            {label: '已报名人数', name: 'signUpCount', index: 'sign_up_count', width: 80, align: "center"},
            {
                label: '状态', name: 'state', index: 'state', width: 80, align: "center", formatter: function (value) {
                    return value === 1 ?
                        '<span class="label label-success">启用</span>' :
                        '<span class="label label-danger">关闭</span>';
                }
            },
            {label: '创建时间', name: 'createDate', index: 'create_date', width: 100, align: "center"},
            {label: '最后更新时间', name: 'lastModifyDate', index: 'last_modify_date', width: 100, align: "center"}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
    $('#contentDesc').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: '300px', //高度
        minHeight: '200px',
        language: "zh_cn",
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../../sys/oss/upload',
        imageUploadParams: {id: "edit"},
        imagesLoadURL: '../../sys/oss/queryAll'
    })
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        signUpSubject: {
            sortOrder: 1
        },
        ruleValidate: {
            sortOrder: [
                {required: true, message: '排序号不能为空', trigger: 'blur', type: 'number'}
            ],
            subjectId: [
                {required: true, message: '科目不能为空', trigger: 'change', type: 'number'}
            ],
            subjectNumbering: [
                {required: true, message: '科目编号不能为空', trigger: 'blur'}
            ],
            subjectQuota: [
                {required: true, message: '报名名额不能为空', trigger: 'change', type: 'number'}
            ],
            subjectPrice: [
                {required: true, message: '科目价格不能为空', trigger: 'change', type: 'number'}
            ],
        },
        q: {
            name: ''
        },
        subjectList: []
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.signUpSubject = {
                state: '1',
                sortOrder: 1,
                subjectQuota: 100,
                subjectPrice: 1000,
            };
            $('#contentDesc').editable('setHTML', '');
            vm.getSignUpSubjectList();
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getSignUpSubjectList();
            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.signUpSubject.id == null ? "lanling/signupsubject/save" : "lanling/signupsubject/update";
            vm.signUpSubject.subjectDetail = $('#contentDesc').editable('getHTML');
            if (vm.signUpSubject.subjectDetail === "" || vm.signUpSubject.subjectDetail == null || vm.signUpSubject.subjectDetail === undefined) {
                alert('科目详情');
                return;
            }
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(vm.signUpSubject),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "lanling/signupsubject/delete",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "lanling/signupsubject/info/" + id, function (r) {
                vm.signUpSubject = r.signUpSubject;
                vm.signUpSubject.state = vm.signUpSubject.state + '';

                $('#contentDesc').editable('setHTML', vm.signUpSubject.subjectDetail);

            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getSignUpSubjectList: function () {
            $.get("../../lanling/subject/queryList", function (r) {
                vm.subjectList = r.list;
            });
        },
        subjectIdChange: function (val) {
            vm.signUpSubject.signSubjectName = val.label.trim();
            vm.signUpSubject.subjectNumbering = val.label.trim();
        }
    }
});